package com.gzhryc.common;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberTools {

	/**
	 * 数字四舍五入
	 * 
	 * @param num
	 * @param decimal
	 * @return
	 */
	public static Float round(Float num, Integer decimal) {
		if (num != null && decimal != null) {
			BigDecimal bg = new BigDecimal(num);
			return bg.setScale(decimal, BigDecimal.ROUND_HALF_UP).floatValue();
		}
		return 0f;
	}

	/**
	 * 数字四舍五入
	 * 
	 * @param num
	 * @param decimal
	 * @return
	 */
	public static Double round(Double num, Integer decimal) {
		if (num != null && decimal != null) {
			BigDecimal bg = new BigDecimal(num);
			return bg.setScale(decimal, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return 0.0;
	}

	/**
	 * 显示金钱（两位小数）
	 * 
	 * @param price
	 * @return
	 */
	public static String getMoneyStr(Double price) {
		return getFormatStr(price, "##.##", "0.00");
	}

	public static String getMoneyLiStr(Double price) {
		return getFormatStr(price, "##.####", "0.0000");
	}

	public static String getMoneyStr(Integer price) {
		Double temp = changeMoney(price);
		return getFormatStr(temp, "##.##", "0.00");
	}

	public static String getMoneyLiStr(Long price) {
		Double temp = changeMoneyLi(price);
		return getFormatStr(temp, "##.####", "0.0000");
	}

	public static String getFormatStr(Double price,Integer unit){
		if(unit == 0) {
			return getFormatStr(price, "##", "0");
		}else if(unit == 1){
			return getFormatStr(price, "##.#", "0.0");
		}else if(unit == 2){
			return getFormatStr(price, "##.##", "0.00");
		}else if(unit == 3){
			return getFormatStr(price, "##.###", "0.000");
		}else {
			return getFormatStr(price, "##.####", "0.0000");
		}
	}

	public static String getMoneyStr(Integer price,Integer unit) {
		Double temp = changeMoney(price);
		return getFormatStr(temp,unit);
	}

	public static String getMoneyLiStr(Long price,Integer unit) {
		Double temp = changeMoneyLi(price);
		return getFormatStr(temp,unit);
	}

	public static String getFormatStr(Double price, String format, String defaultStr) {
		if (price != null) {
			NumberFormat nf = new DecimalFormat(format);
			if (price >= 0) {
				return nf.format(price);
			} else {
				return "-" + nf.format(Math.abs(price));
			}
		} else {
			return defaultStr;
		}
	}

	/**
	 * 转化钱的数据类型
	 * 
	 * @param price
	 * @return
	 */
	public static Integer changeMoney(Double price) {
		if (price != null) {
			BigDecimal result = BigDecimal.valueOf(price).multiply(new BigDecimal(100D));
			return result.intValue();
		}
		return 0;
	}

	public static Long changeMoneyLi(Double price) {
		if (price != null) {
			BigDecimal result = BigDecimal.valueOf(price).multiply(new BigDecimal(10000D));
			return result.longValue();
		}
		return 0L;
	}

	/**
	 * 转化钱的数据类型，将分转化成double型
	 * 
	 * @param price
	 * @return
	 */
	public static Double changeMoney(Integer price) {
		if (price != null) {
			BigDecimal priceBigDecimal = new BigDecimal(price);
			BigDecimal result = priceBigDecimal.divide(new BigDecimal(100D), 2, BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	public static Double changeMoneyLi(Long price) {
		if (price != null) {
			BigDecimal priceBigDecimal = new BigDecimal(price);
			BigDecimal result = priceBigDecimal.divide(new BigDecimal(10000D), 4, BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	/**
	 * 两个数相乘
	 * @param value
	 * @param multiple
	 * @return
	 */
	public static Integer multiply(Double value,Integer multiple){
		if(value != null && multiple != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.multiply(new BigDecimal(multiple));
			return result.intValue();
		}
		return 0;
	}

	public static Long multiply(Double value,Long multiple){
		if(value != null && multiple != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.multiply(new BigDecimal(multiple));
			return result.longValue();
		}
		return 0L;
	}
	
	public static Double multiply(Double value,Double multiple){
		if(value != null && multiple != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.multiply(new BigDecimal(multiple));
			return result.doubleValue();
		}
		return 0D;
	}

	/**
	 * 两个数相除
	 * @param value
	 * @param multiple
	 * @param scale
	 * @return
	 */
	public static Double divide(Integer value,Integer multiple,Integer scale){
		if(value != null && multiple != null && scale != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.divide(new BigDecimal(multiple),scale,BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	public static Double divide(Long value,Long multiple,Integer scale){
		if(value != null && multiple != null && scale != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.divide(new BigDecimal(multiple),scale,BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	public static Double divide(Long value,Integer multiple,Integer scale){
		if(value != null && multiple != null && scale != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.divide(new BigDecimal(multiple),scale,BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	public static Double divide(Double value,Double multiple,Integer scale){
		if(value != null && multiple != null && scale != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.divide(new BigDecimal(multiple),scale,BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	public static Double divide(Double value,Integer multiple,Integer scale){
		if(value != null && multiple != null && scale != null) {
			BigDecimal bigDecimal = new BigDecimal(value);
			BigDecimal result = bigDecimal.divide(new BigDecimal(multiple),scale,BigDecimal.ROUND_DOWN);
			return result.doubleValue();
		}
		return 0.0;
	}

	/**
	 * 计算折扣
	 * 
	 * @param money
	 * @param rebate
	 * @return
	 */
	public static Integer discount(Integer money, Double rebate) {
		if (money != null && rebate != null) {
			BigDecimal moneyBigDecimal = new BigDecimal(money);
			moneyBigDecimal = moneyBigDecimal.divide(new BigDecimal(100D), 2, BigDecimal.ROUND_DOWN);

			BigDecimal rebateBigDecimal = new BigDecimal(rebate);
			rebateBigDecimal = rebateBigDecimal.divide(new BigDecimal(10D));

			moneyBigDecimal = moneyBigDecimal.multiply(rebateBigDecimal);

			return moneyBigDecimal.multiply(new BigDecimal(100D)).intValue();
		}
		return 0;
	}

	public static Long discount(Long money, Double rebate) {
		if (money != null && rebate != null) {
			BigDecimal moneyBigDecimal = new BigDecimal(money);
			moneyBigDecimal = moneyBigDecimal.divide(new BigDecimal(100D), 2, BigDecimal.ROUND_DOWN);

			BigDecimal rebateBigDecimal = new BigDecimal(rebate);
			rebateBigDecimal = rebateBigDecimal.divide(new BigDecimal(10D));

			moneyBigDecimal = moneyBigDecimal.multiply(rebateBigDecimal);

			return moneyBigDecimal.multiply(new BigDecimal(100D)).longValue();
		}
		return 0L;
	}

	/**
	 * 计算比例
	 * 
	 * @param money
	 * @param proportion
	 * @return
	 */
	public static Integer proportion(Integer money, Double proportion) {
		if (money != null && proportion != null) {
			BigDecimal moneyBigDecimal = new BigDecimal(money);
			moneyBigDecimal = moneyBigDecimal.divide(new BigDecimal(100D), 2, BigDecimal.ROUND_DOWN);

			BigDecimal rebateBigDecimal = new BigDecimal(proportion);
			rebateBigDecimal = rebateBigDecimal.divide(new BigDecimal(100D));

			moneyBigDecimal = moneyBigDecimal.multiply(rebateBigDecimal);

			return moneyBigDecimal.multiply(new BigDecimal(100D)).intValue();
		}
		return 0;
	}

	public static Long proportion(Long money, Double proportion) {
		if (money != null && proportion != null) {
			BigDecimal moneyBigDecimal = new BigDecimal(money);
			moneyBigDecimal = moneyBigDecimal.divide(new BigDecimal(100D), 2, BigDecimal.ROUND_DOWN);

			BigDecimal rebateBigDecimal = new BigDecimal(proportion);
			rebateBigDecimal = rebateBigDecimal.divide(new BigDecimal(100D));

			moneyBigDecimal = moneyBigDecimal.multiply(rebateBigDecimal);

			return moneyBigDecimal.multiply(new BigDecimal(100D)).longValue();
		}
		return 0L;
	}

	/**
	 * 计算百分比
	 * 
	 * @param num1
	 * @param num2
	 * @return
	 */
	public static Double percentage(Integer num1, Integer num2) {
		if (num2 != 0) {
			BigDecimal a = new BigDecimal(num1);
			BigDecimal b = new BigDecimal(num2);
			BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP);
			return c.multiply(new BigDecimal(100)).doubleValue();
		}
		return 0.0;
	}

	public static Double percentage(Long num1, Long num2) {
		if (num2 != 0) {
			BigDecimal a = new BigDecimal(num1);
			BigDecimal b = new BigDecimal(num2);
			BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP);
			return c.multiply(new BigDecimal(100)).doubleValue();
		}
		return 0.0;
	}

	public static Double percentage(Double num1, Double num2) {
		if (num2 != 0) {
			BigDecimal a = new BigDecimal(num1);
			BigDecimal b = new BigDecimal(num2);
			BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP);
			return c.multiply(new BigDecimal(100)).doubleValue();
		}
		return 0.0;
	}

	/**
	 * 计算平均
	 * 
	 * @param num1
	 * @param count
	 * @return
	 */
	public static Double average(Integer num1, Integer count) {
		if (count != 0) {
			BigDecimal a = new BigDecimal(num1);
			BigDecimal b = new BigDecimal(count);
			return a.divide(b, 2, RoundingMode.HALF_UP).doubleValue();
		}
		return 0.0;
	}

	public static Double average(Double num1, Integer count) {
		if (count != 0) {
			BigDecimal a = new BigDecimal(num1);
			BigDecimal b = new BigDecimal(count);
			return a.divide(b, 2, RoundingMode.HALF_UP).doubleValue();
		}
		return 0.0;
	}

}
